package com.eva.cms.biz.search;

import com.eva.cms.biz.article.CmsArticleBiz;
import com.eva.cms.biz.article.dto.QueryCmsArticleDTO;
import com.eva.cms.biz.article.vo.CmsArticleProfileVO;
import com.eva.cms.biz.search.vo.SearchResultVO;
import com.eva.constants.CmsArticleStatusEnum;
import com.eva.framework.common.model.PageData;
import com.eva.framework.common.model.PageWrap;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class DefaultSearchBiz {

    @Resource
    private CmsArticleBiz cmsArticleBiz;

    /**
     * 站内分页搜索
     *
     * @param pageWrap 搜索参数
     * @return PageData<CmsArticleProfileVO>
     */
    public SearchResultVO search (PageWrap<String> pageWrap) {
        // 增加页容量判断，页容量最大100（防止恶意用户给定超大参数）
        if (pageWrap.getCapacity() > 100) {
            pageWrap.setCapacity(10);
        }
        // 搜索文章
        PageWrap<QueryCmsArticleDTO> queryWrap = new PageWrap<>();
        queryWrap.setPage(pageWrap.getPage());
        queryWrap.setCapacity(pageWrap.getCapacity());
        queryWrap.setModel(QueryCmsArticleDTO.builder()
                .keyword(pageWrap.getModel())
                .status(CmsArticleStatusEnum.ONLINE.name())
                .build());
        // 构建搜索结果
        SearchResultVO result = new SearchResultVO();
        result.setArticlePage(cmsArticleBiz.findProfilePage(queryWrap));
        return result;
    }
}
